package com.kun.oa.controller;

import com.github.pagehelper.PageInfo;
import com.kun.oa.vo.RespStat;
import com.kun.oa.entity.Account;
import com.kun.oa.entity.Config;
import com.kun.oa.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Classname AccountController
 * @Created by FangKun
 * @Date 2023/1/2 0002 下午 16:42
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    @Autowired
    Config config;

    /**
     * 跳转注册页
     * @return
     */
    @RequestMapping("/register")
    public String register(){
        return "account/register";
    }

    /**
     * 用户注册
     * @param account
     * @return
     */
    @PostMapping("/registerUser")
    public String registerUser(Account account,Model model){

        boolean res = accountService.registerUser(account);
        if (res){
            return "account/login";
        }else {
            model.addAttribute("msg","注册失败，请重试！");
            return "errorPage";
        }
    }

    /**
     * 跳转登录页
     * @param model
     * @return
     */
    @RequestMapping("login")
    public String login(Model model) {
        return "account/login";
    }

    /**
     * 登录验证
     * @param loginName
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/validataAccount")
    @ResponseBody
    public String validataAccount(String loginName, String password, HttpServletRequest request){
        Account account = accountService.validataAccount(loginName,password);
        if (account == null){
            return "登录失败";
        } else {
            request.getSession().setAttribute("account",account);
            return "success";
        }
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request){
        request.getSession().removeAttribute("account");
        return "index";
    }

    /**
     * 用户列表分页查询
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, Model model){
        PageInfo<Account> accountList = accountService.findByPage(pageNum,pageSize);
        model.addAttribute("page", accountList);
        return "account/list";
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public RespStat deleteById( int id) {
        // 标记一下 是否删除成功？  status
        RespStat respStat = accountService.deleteById(id);
        return respStat;
    }

    /**
     * 个人信息
     * @return
     */
    @RequestMapping("/profile")
    public String profile () {
        return "account/profile";
    }

    /**
     * 个人信息修改
     * @param filename
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/fileUploadController")
    public String fileUpload (MultipartFile filename,String password,String nickName,Integer age,HttpServletRequest request) {

        Account account = (Account) request.getSession().getAttribute("account");
        try {
            /*String fileName = System.currentTimeMillis() + filename.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "avator";
            File dest = new File(filePath + System.getProperty("file.separator") + fileName);

            filename.transferTo(dest);
            String storeAvatorPath = "/avator/"+fileName;*/

            //指定目录
            filename.transferTo(new File("E:/springboot-oa/avator/" + filename.getOriginalFilename()));

            account.setPassword(password);
            account.setLocation(filename.getOriginalFilename());
            account.setAge(age);
            account.setNickName(nickName);

            accountService.updateMsg(account);

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "account/profile";
    }

    /**
     * 根据id查询要修改用户的信息
     * @param id
     * @return
     */
    @PostMapping("/queryModifyUser")
    @ResponseBody
    public RespStat queryModifyUser(Integer id, HttpServletRequest request){
        Account account = accountService.selectById(id);

        if (account == null){
            return RespStat.build(500,"操作失败啦！");
        } else {
            request.getSession().setAttribute("modifyAccount",account);
            return RespStat.build(200);
        }
    }
}














